﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Popfly.GameCreator.GameEngine;
using Microsoft.Popfly.GameCreator.GameEngine.Collisions;

public abstract class DebugCollisionEdges
{    
    public DebugCollisionEdges(Shape shape, bool isSolid)
    {
        Debug.Assert(shape != null, "Argument shape cannot be null.");

        Edges = shape;
        Edges.IsHitTestVisible = false;
        Edges.StrokeThickness = 2;
        Edges.Stroke = new SolidColorBrush(isSolid ? Colors.Red : Colors.Blue);
        Edges.Fill = new SolidColorBrush(isSolid ? new Color { A = 50, R = 0, G = 255, B = 255 } : new Color { A = 50, R = 255, G = 127, B = 237 });

        Canvas = new Canvas();
        Canvas.SetValue(Canvas.ZIndexProperty, 2);
        Canvas.Children.Add(Edges);
    }

    public static DebugCollisionEdges Create(CollisionEdges.EdgesType type, bool isSolid)
    {
        switch (type)
        {
            case CollisionEdges.EdgesType.CIRCLE:
                return new EllipseDebugCollisionEdges(isSolid);
            case CollisionEdges.EdgesType.RECT:
                return new RectangleDebugCollisionEdges(isSolid);
            default:
                throw new NotSupportedException(String.Format("CollisionEdges.EdgesType {0} is not supported", type));
        }
    }

    public Canvas Canvas { get; set; }

    public Shape Edges { get; set; }

    public void Draw(bool showEdges, CollisionBounds bounds)
    {
        Debug.Assert(Canvas != null, "Canvas cannot be null.");

        if (showEdges)
        {
            Canvas.Visibility = Visibility.Visible;

            OnDraw(bounds);

            Canvas.Height = (double) Edges.GetValue(Shape.ActualHeightProperty);
            Canvas.Width = (double) Edges.GetValue(Shape.ActualWidthProperty);
            Canvas.SetValue(Canvas.TopProperty, bounds.Y);
            Canvas.SetValue(Canvas.LeftProperty, bounds.X);
        }
        else
        {
            Canvas.Visibility = Visibility.Collapsed;
        }
    }

    public abstract void OnDraw(CollisionBounds bounds);
}

public class EllipseDebugCollisionEdges : DebugCollisionEdges
{
    public EllipseDebugCollisionEdges(bool isSolid)
        : base(new Ellipse(), isSolid)
    {
    }

    public override void OnDraw(CollisionBounds bounds)
    {
        var diameter = bounds.Radius * 2;
        Edges.SetValue(Ellipse.HeightProperty, diameter);
        Edges.SetValue(Ellipse.WidthProperty, diameter);
    }
}

public class RectangleDebugCollisionEdges : DebugCollisionEdges
{
    public RectangleDebugCollisionEdges(bool isSolid)
        : base(new Rectangle(), isSolid)
    {
    }

    public override void OnDraw(CollisionBounds bounds)
    {
        Edges.SetValue(Rectangle.HeightProperty, bounds.Height);
        Edges.SetValue(Rectangle.WidthProperty, bounds.Width);
    }
}